package com.voja.mrvica2.fragment;

import com.voja.mrvica2.Entry;
import com.voja.mrvica2.Index;
import com.voja.mrvica2.R;
import com.voja.mrvica2.remoteimage.RemoteImageView;
import com.voja.mrvica2.remoteimage.RemoteImageZoom;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailsFragment extends Fragment {
    
    private Entry entry;
    private Index activity;
    
    public static int originalImageWidth, originalImageHeight;
    
    public DetailsFragment() {}
    
    public void setParams(Index activity, Entry entry) {
    	this.activity = activity;
    	this.entry    = entry;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        
        super.onActivityCreated(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle instanceState) {
        View v = inflater.inflate(R.layout.item_details, null);
        
        if (entry != null) {
            TextView title = (TextView)v.findViewById(R.id.itemTitle);
            title.setText(entry.getTitle());
            
            RemoteImageView image = (RemoteImageView)v.findViewById(R.id.itemImage);
            image.setImage(entry.getImage());
            image.setOnClickListener(new RemoteImageZoom(activity));
            
            TextView desc = (TextView)v.findViewById(R.id.itemDescription);
            desc.setText(entry.getDescription());
            
            TextView link = (TextView)v.findViewById(R.id.itemLink);
            final String itemLink = entry.getLink();
            link.setText(itemLink);
            link.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(itemLink));
                    startActivity(intent);
                }
            });
        }
        
        return v;
    }
    
    public Entry getEntry() {
        return entry;
    }
}
